package com.gitee.xmhzzz.component.spring.cloud.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

public class XmhWebMvcRegistrations implements org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations
{
    
    private static final Logger LOG = LoggerFactory.getLogger(XmhWebMvcRegistrations.class);
    
    @Override
    public RequestMappingHandlerMapping getRequestMappingHandlerMapping()
    {
        return new FeignRequestMappingHandlerMapping();
    }
    
    private static class FeignRequestMappingHandlerMapping extends RequestMappingHandlerMapping
    {
        @Override
        protected boolean isHandler(Class<?> beanType)
        {
            //有FeignClient和RestController配合使用才认为是一个服务端提供出去的接口
            //没有FeignClient注解，则判断系统默认规则
            LOG.debug("Dealing with type = " + beanType.getName());
            LOG.debug("Has FeignClient Annotation ? " + AnnotatedElementUtils.hasAnnotation(beanType, FeignClient.class));
            LOG.debug("Has RestController Annotation ? " + AnnotatedElementUtils.hasAnnotation(beanType, RestController.class));
            return (AnnotatedElementUtils.hasAnnotation(beanType, FeignClient.class) && AnnotatedElementUtils.hasAnnotation(beanType, RestController.class))
            || (super.isHandler(beanType) && !AnnotatedElementUtils.hasAnnotation(beanType, FeignClient.class));
            
        }
        
    }
}
